package rhinestone.Model;

import java.util.HashMap;
import java.io.Serializable;

/**
 * The container of all employees.
 *
 * @author Group 2
 * @version 2010.12.16
 */
public class StaffContainer implements Serializable {
	private HashMap<String, Staff> employees;
	private static StaffContainer instance;
	private boolean changed;

	/**
	 * Private constructor which initializes instance variables.
	 */
	private StaffContainer() {
		employees = new HashMap<String, Staff>();
		changed = false;
	}

	/**
	 * Singleton pattern.
	 *
	 * @return The object of this container.
	 */
	public static StaffContainer getInstance() {
		if(instance == null)
			instance = new StaffContainer();
		return instance;
	}

	public void addStaff(Staff staff) {
		if(!employees.containsKey(staff.getCpr())) {
			employees.put(staff.getCpr(), staff);
			System.out.println(" --- Employee added ---");
			changed = true;
		}
		else
			System.err.println("Employee with CPR " + staff.getCpr() + " already exists.");
	}

	/**
	 * Finds and returns an Employee object with provided CPR.
	 *
	 * @param cpr CPR of the employee.
	 * @return The Staff object (null if not found).
	 */
	public Staff getStaff(String cpr) {
		return employees.get(cpr);
	}

	public Staff removeStaff(String cpr) {
		changed = true;
		return employees.remove(cpr);
	}

	public boolean isChanged() {
		return changed;
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}
}
